package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

public class PageFilter<T> extends Page<T> {

    private T condation;

    private String[] columns = new String[] {};

    public QueryWrapper<T> toPageWrapper() {

        QueryWrapper<T> wrapper = new QueryWrapper<>();

        if (eqMap.size() > 0) {
            wrapper.allEq(eqMap);
        }
        if (nqMap.size() > 0) {
            nqMap.forEach((column, val) -> {
                wrapper.ne(column, val);
            });
        }
        if (gtMap.size() > 0) {
            gtMap.forEach((column, val) -> {
                wrapper.gt(column, val);
            });
        }
        if (geMap.size() > 0) {
            geMap.forEach((column, val) -> {
                wrapper.ge(column, val);
            });
        }
        if (ltMap.size() > 0) {
            ltMap.forEach((column, val) -> {
                wrapper.lt(column, val);
            });
        }

        if (leMap.size() > 0) {
            leMap.forEach((column, val) -> {
                wrapper.le(column, val);
            });
        }

        if (betweenMap.size() > 0) {
            betweenMap.forEach((column, vals) -> {
                if (vals == null || vals.size()!= 2) {
                    throw new RuntimeException("betweenMap params error !");
                }
                wrapper.between(column, vals.get(0), vals.get(1));
            });
        }

        if (likeMap.size() > 0) {
            likeMap.forEach((column, val) -> {
                wrapper.like(column, val);
            });
        }
        return wrapper.select(columns);
    }

    /**
     * 等于 =
     */
    public Map<String, Object> eqMap = Maps.newHashMap();


    /**
     * 不等于 <>
     */
    public Map<String, Object> nqMap = Maps.newHashMap();

    /**
     * 大于 >
     *
     * @param condition 执行条件
     */
    public Map<String, Object> gtMap = Maps.newHashMap();


    /**
     * 大于 >=
     *
     * @param condition 执行条件
     */
    public Map<String, Object> geMap = Maps.newHashMap();

    /**
     * 小于 <
     *
     * @param condition 执行条件
     */
    public Map<String, Object> ltMap = Maps.newHashMap();

    /**
     * 小于等于 <=
     *
     * @param condition 执行条件
     */
    public Map<String, Object> leMap = Maps.newHashMap();


    /**
     * between =
     *
     * @param condition 执行条件
     */
    public Map<String, List<Object>> betweenMap = Maps.newHashMap();


    /**
     * LIKE '%值%'
     */
    public Map<String, Object> likeMap = Maps.newHashMap();


    public String[] getColumns() {
        return columns;
    }

    public void setColumns(String[] columns) {
        this.columns = columns;
    }

    public T getCondation() {
        return condation;
    }

    public void setCondation(T condation) {
        this.condation = condation;
    }

}
